home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 5791 / 5791.xpi / chrome / flagfox.jar / content / context.js < prev    next >
Text File  |  2009-05-21  |  6KB  |  154 lines

  1. function Flagfox_handleClick(event)
  2. {
  3.     switch (event.button)
  4.     {
  5.         case 0:
  6.             if (!event.ctrlKey)
  7.             {
  8.                 Flagfox_handleAction("Geotool");
  9.                 return;
  10.             } // else fallthrough; ctrl+click == middle-click
  11.         case 1:
  12.             Flagfox_handleAction(FFpreferences.getCharPref("flagfox.middleclick"));  // Set action via options
  13.             return;
  14.         // Button 2 shows popup menu via context attribute
  15.     }
  16. }
  17.  
  18. function Flagfox_handleCursor(imagenode)  // Changes mouseover cursor to a hand when there is a click action
  19. {
  20.     imagenode.style.cursor = Flagfox_isActionAllowed("Geotool") ? "pointer" : "default" ;
  21. }
  22.  
  23. function Flagfox_handleMenu(menupopup)
  24. {
  25.     // Decide which menu items to grey out
  26.     var menuItems = menupopup.getElementsByTagName("menuitem");
  27.     for (var i=0; i < menuItems.length; i++)  // menuItems[i].value is not always reliable
  28.         menuItems[i].setAttribute( "disabled", !Flagfox_isActionAllowed(menuItems[i].getAttribute("value")) );
  29.  
  30.     // Handle custom action
  31.     var customMenuItem = document.getElementById("flagfox-customactionmenuitem");
  32.     customMenuItem.hidden = !FFpreferences.getBoolPref("flagfox.customlookup.enabled");
  33.     customMenuItem.label = Flagfox_getUCharPref(FFpreferences,"flagfox.customlookup.name");
  34. }
  35.  
  36. function Flagfox_isActionAllowed(action)
  37. {
  38.     function isPrivateIP()
  39.     {
  40.         return flagState.country != null && flagState.country[0][0] == '-';  // -A, -B, -C, -L
  41.     }
  42.  
  43.     switch (action)
  44.     {
  45.         case "Geotool":   return flagState.ip != "" && !isPrivateIP() && !flagState.IPisV6();  // Needs a non-private v4 IP
  46.         case "Wikipedia": return flagState.country != null;                                    // Needs a fully looked-up location
  47.         case "Whois":     return flagState.host != "" && !isPrivateIP();                       // Just needs a host, but can't be a private IP
  48.         case "CopyIP":    return flagState.ip != "";                                           // Just needs any IP
  49.  
  50.         case "Custom":
  51.             var customURL = Flagfox_getUCharPref(FFpreferences,"flagfox.customlookup.url");
  52.             var needsCountry = customURL.search(/{countryName}/i)!=-1 || customURL.search(/{countryCode}/i)!=-1;
  53.             var needsHost = customURL.search(/{domainName}/i)!=-1;
  54.             var needsIP = customURL.search(/{IPaddress}/i)!=-1;
  55.             if ( (needsCountry && flagState.country == null) ||
  56.                  (needsHost && flagState.host == "") ||
  57.                  (needsIP && flagState.ip == "") )
  58.                 return false;
  59.             else
  60.                 return true;
  61.  
  62.         case "Options": return true;
  63.         case "Nothing": return false;  // Set "flagfox.middleclick" to this to disable that feature
  64.  
  65.         default: return false;
  66.     }
  67. }
  68.  
  69. function Flagfox_handleAction(action)
  70. {
  71.     if (!Flagfox_isActionAllowed(action))
  72.         return;  // Disallowed or invalid action
  73.  
  74.     switch (action)
  75.     {
  76.         case "Geotool":
  77.             // Geotool figures out the locale from the HTTP header
  78.             // We send ip and host in case Geotool resolves to a different server, based on its different location
  79.             Flagfox_openLink("http://geotool.flagfox.net/?ip=" + flagState.ip + "&host=" + flagState.host);
  80.             return;
  81.  
  82.         case "Wikipedia":
  83.             var wiki;
  84.             switch (flagState.country[0])
  85.             {
  86.                 case '-A':  case '-B':  case '-C':
  87.                     wiki = "RFC 1918";
  88.                     break;
  89.                 default:  // For 'A1', 'A2', and '-L' flagState.country[1] == translated range description
  90.                     wiki = flagState.country[1];
  91.                     break;
  92.             }
  93.             Flagfox_openLink(Flagfox_getWikipediaSearchURL(wiki));
  94.             return;
  95.  
  96.         case "Whois":
  97.             // DomainTools can parse TLD correctly, as well as full domain names
  98.             Flagfox_openLink("http://whois.domaintools.com/" + flagState.host);
  99.             return;
  100.  
  101.         case "CopyIP":
  102.             const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
  103.                                               .getService(Components.interfaces.nsIClipboardHelper);
  104.             clipboardHelper.copyString(flagState.ip);
  105.             return;
  106.  
  107.         case "Custom":
  108.             /* Both custom URL and country name need encoding and I can't do encodeURL() for the country name
  109.                as that ignores certain characters that might cause problems with searches. To prevent double
  110.                encoding I do encodeURL() first and simply search using encoded placeholders.
  111.                This is case-insensitive and placeholders may be used multiple times. */
  112.             var customURL = encodeURI(Flagfox_getUCharPref(FFpreferences,"flagfox.customlookup.url"));
  113.             customURL = customURL.replace(/%7BdomainName%7D/gi, flagState.host)
  114.                                  .replace(/%7BIPaddress%7D/gi, flagState.ip);
  115.             if (flagState.country != null)
  116.                 customURL = customURL.replace(/%7BcountryName%7D/gi, encodeURIComponent(flagState.country[1]))
  117.                                      .replace(/%7BcountryCode%7D/gi, flagState.country[0]);
  118.             Flagfox_openLink(customURL);
  119.             return;
  120.  
  121.         case "Options":
  122.             window.openDialog("chrome://flagfox/content/options.xul", "FlagfoxOptions", "chrome,dialog,centerscreen");
  123.             return;
  124.     }
  125. }
  126.  
  127. function Flagfox_openLink(url)
  128. {
  129.     try
  130.     {
  131.         var tabPrefs = FFpreferences.getCharPref("flagfox.openlinksin");
  132.  
  133.         if (tabPrefs == "tabBG" || tabPrefs == "tabFG")
  134.         {
  135.             var newTab = window.getBrowser().addTab(url,null,null);
  136.             if (tabPrefs == "tabFG")
  137.                 window.getBrowser().selectedTab = newTab;
  138.         }
  139.         else if (tabPrefs == "currentTab")
  140.         {
  141.             window.content.document.location = url;
  142.         }
  143.         else  // "winBG" || "winFG"
  144.         {
  145.             var newWindow = window.open(url,"_blank");
  146.             if (tabPrefs == "winBG")
  147.             {
  148.                 newWindow.blur();
  149.                 window.focus();
  150.             }
  151.         }
  152.     } catch (e) { Flagfox_error("Failed to open URL: "+url,e); }
  153. }
  154.